//客户资料管理

interface IReturnPeriodViewModel extends ng.IScope {
    /**
     * 对账周期
     */
    reconciliationCycleTime:string;
    /**
     * 对账周期单位
     */
    reconciliationCycleUnit:string;
    /**
     * 回款周期
     */
    returnPeriodTime:string;
    /**
     * 回款周期单位
     */
    returnPeriodUnit:string;
    /**
     * 时间下拉（日，月）
     */
    timeList:any[];
    /**
     * 点击事件
     */
    click: () => void;
    
}


class ReturnPeriodController {
    /**
     * 客户单位id
     */
    id:string;

    constructor(private $scope: IReturnPeriodViewModel, private accountCycleService: IAccountCycleService, private valueService: IValueService,
        private $ngBootbox: BootboxService, private $location: ng.ILocationService,private clientService:IClientService) {
        this.init();
    }

    init(): void {
        this.loadTypeDropDown();

        this.id = this.$location.search().id;

        if(!this.id){
             this.id = JSON.parse(window.localStorage.getItem("client").toString()).clientId;
        }else{
            this.id = this.id;
        }

        this.clientService.getDetail(this.id).then(result => {
            let client = {
                clientId:this.id,
                clientName: result.data.name
            }
            window.localStorage.setItem("client",JSON.stringify(client));

            this.$scope.reconciliationCycleTime = result.data.accountCycle;
            this.$scope.reconciliationCycleUnit = result.data.accountCycleUnit;
            this.$scope.returnPeriodTime = result.data.backMoneyCycle;
            this.$scope.returnPeriodUnit = result.data.backMoneyCycleUnit;
            // this.$scope.click = this.saveTime;
            // this.loadServiceOfficerDropDown();
            // this.loadBusinessOfficerDropDown();
        });


        //保存
        this.$scope.click=this.saveTime;
    }

    // loadData(queryTerms: IClientInfoManagentQueryTerms, skip: number, count: number): void {
    //     this.clientInfoService.getList(this.enterpriseId, queryTerms.name, queryTerms.type, queryTerms.level, skip, count).then(result => {
    //         this.$scope.gridOptions.totalItems = result.data.total;
    //         result.data.data.map((item, index) => item.index = skip++ +1);
    //         this.$scope.gridOptions.data = result.data.data;

    //         this.$scope.heightA = (this.$scope.gridOptions.data.length *39)+95;

    //         if (skip == 0) {
    //             this.$scope.gridOptions.paginationCurrentPage = 1;
    //         }
    //         this.$scope.querying = false;
    //     }, error => {
    //         this.$scope.querying = false;
    //     });
    // }


    /**
     * 读取资料类别下拉数据
     */
    loadTypeDropDown(): void {
        this.$scope.timeList = this.valueService.getTimeType().data.slice(1);
        console.log('dd',this.$scope.timeList)
    }


    // showAddClientInfoDialog = () => {
    //     this.$scope.title = "添加客户资料";
    //     this.$scope.detail = {
    //         name: "",
    //         type: "",
    //         level: "",
    //         content: "",
    //         click: this.addClientInfo
    //     };
    //     $("#content").html("");
    //     this.$scope.attachmentUploader.queue = [];
    //     this.uploaderList = [];
    // }

    saveTime=()=> {
        // if (!$("#returnPeriodForm").valid()) {  }
        console.log('d',this.$scope.reconciliationCycleTime)
        //!this.$scope.reconciliationCycleTime || 
        if( this.$scope.reconciliationCycleTime==undefined || this.$scope.reconciliationCycleTime && !this.$scope.reconciliationCycleTime.match(/^\d+$/)){
            this.$ngBootbox.alert("请输入不小于0的整数");
            return;
        }
        if( this.$scope.returnPeriodTime==undefined || this.$scope.returnPeriodTime &&  !this.$scope.returnPeriodTime.match(/^\d+$/)){
            this.$ngBootbox.alert("请输入不小于0的整数");
            return;
        }

        //都为空或者都有值才可以保存
        
        this.$ngBootbox.confirm("确定保存此数据吗？").then(e => {
            this.accountCycleService.saveEnterpriseCycle(
                this.id,
                this.$scope.reconciliationCycleTime,
                this.$scope.reconciliationCycleUnit,
                this.$scope.returnPeriodTime,
                this.$scope.returnPeriodUnit
            ).then(result => {
                // this.reload();
                // this.uploaderList = [];
                // this.$scope.allowAttachmentUploader = true;
                // $("#clientinfo").modal("hide");
            });
        });
    }

}


angular.module("tms").controller("returnPeriodController", ["$scope", "accountCycleService", "valueService", "$ngBootbox", "$location","clientService", ReturnPeriodController]);


// http://192.168.0.25:5005/tms/attachmentdownload/9?token=6364325256403177988756E3427588C64D130107812B34CC9D&fileId=14e5ece8-21ae-42bf-bbcb-200b43c8ab3b